package topic.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseActivity;
import common.Constant;
import topic.activity.DeliverComment;
import topic.activity.SecondComment;
import topic.bean.TopicDetailBean;
import personalPage.activity.PersonalPage;
import utils.MyLogUtils;
import utils.SPUtils;
import utils.ToastUtils;
import view.MyClickableSpan;
import view.MyConfirmDialog;
import view.MyImageLoader;
import view.MyVariousLineTextView;

/**
 * Created by Administrator on 2017/3/22.
 * 话题详情主页的适配器
 */

public class TopicDetailAdapter extends BaseAdapter {
    private String blogId;
    private Context context;
    private ArrayList<TopicDetailBean.CommentListBean> arrayList;

    private DeleteCommentListener deleteComment;

    public TopicDetailAdapter(Context context, ArrayList<TopicDetailBean.CommentListBean> arrayList, String blogId, DeleteCommentListener deleteComment) {
        this.context = context;
        this.arrayList = arrayList;
        this.blogId = blogId;
        this.deleteComment = deleteComment;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyHolder myHolder;
        if (convertView == null) {
            myHolder = new MyHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.topic_detail_item, null);
            myHolder.topic_detail_item_headLayout = (RelativeLayout) convertView.findViewById(R.id.topic_detail_item_headLayout);
            myHolder.topic_detail_item_time = (TextView) convertView.findViewById(R.id.topic_detail_item_time);
            myHolder.topic_detail_item_content = (TextView) convertView.findViewById(R.id.topic_detail_item_content);
            myHolder.topic_detail_item_head_ima = (RoundedImageView) convertView.findViewById(R.id.topic_detail_item_head_ima);
            myHolder.topic_detail_item_name = (TextView) convertView.findViewById(R.id.topic_detail_item_name);
            myHolder.topic_detail_item_parent_layout = (LinearLayout) convertView.findViewById(R.id.topic_detail_item_parent_layout);
            myHolder.topic_detail_item_second_comment_lv = (ListView) convertView.findViewById(R.id.topic_detail_item_second_comment_lv);
            myHolder.topic_detail_recomment_list_footer = (LinearLayout) convertView.findViewById(R.id.topic_detail_recomment_list_footer);
            myHolder.topic_detail_recomment_list_footer_tv = (TextView) convertView.findViewById(R.id.topic_detail_recomment_list_footer_tv);
            myHolder.topic_detail_item_delete = (TextView) convertView.findViewById(R.id.topic_detail_item_delete);
            convertView.setTag(myHolder);
        } else {
            myHolder = (MyHolder) convertView.getTag();
        }
        myHolder.topic_detail_item_name.setText(arrayList.get(position).getNickname());
        myHolder.topic_detail_item_time.setText(arrayList.get(position).getCreate_time());
        myHolder.topic_detail_item_content.setText(arrayList.get(position).getContent());
        MyImageLoader.showImage(arrayList.get(position).getHeadimgurl(), myHolder.topic_detail_item_head_ima);

        if (arrayList.get(position).getShowdelete() == 0) {
            //隐藏删除
            myHolder.topic_detail_item_delete.setVisibility(View.GONE);
        } else {
            //显示删除
            myHolder.topic_detail_item_delete.setVisibility(View.VISIBLE);
            myHolder.topic_detail_item_delete.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    deleteComment.deleteCommentListener(position);
                }
            });
        }

        //进入个人首页
        myHolder.topic_detail_item_headLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(context, PersonalPage.class);
                i.putExtra("userId", arrayList.get(position).getUser_id());
                i.putExtra("type", "app");
                context.startActivity(i);
            }
        });
        //进入评论首页
        myHolder.topic_detail_item_parent_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (((BaseActivity) context).hasLoad()) {
                    Intent i = new Intent(context, SecondComment.class);
                    i.putExtra("blogid", blogId);
                    i.putExtra("CommentListBean", arrayList.get(position));
                    ((BaseActivity) context).startActivityForResult(i, position);
                } else {
                    ((BaseActivity) context).login(context);
                }
            }
        });

        myHolder.myAdapter = new MyAdapter(arrayList.get(position).getRecommentList());

        if (arrayList.get(position).getCom_count() >= 3) {
            if (myHolder.topic_detail_item_second_comment_lv.getChildCount() < 3) {
                myHolder.topic_detail_recomment_list_footer.setVisibility(View.VISIBLE);
                myHolder.topic_detail_recomment_list_footer_tv.setText("共" + arrayList.get(position).getCom_count() + "条回复");
                myHolder.topic_detail_recomment_list_footer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent();
                        i.setClass(context, SecondComment.class);
                        i.putExtra("blogid", blogId);
                        i.putExtra("CommentListBean", arrayList.get(position));
                        context.startActivity(i);
                    }
                });
            }
        } else {
            myHolder.topic_detail_recomment_list_footer.setVisibility(View.GONE);
        }
        myHolder.topic_detail_item_second_comment_lv.setAdapter(myHolder.myAdapter);
        setPullLvHeight(myHolder.topic_detail_item_second_comment_lv, myHolder.myAdapter, null);

        //点击二级评论并且评论之
        myHolder.topic_detail_item_second_comment_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position1, long id) {
                if (((BaseActivity) context).hasLoad()) {
                    if (!SPUtils.getSharedPreferences().getString(Constant.USER_ID, "").equals(arrayList.get(position).getRecommentList().get(position1).getUser_id())) {
                        //不是评论自己
                        Intent i = new Intent();
                        i.setClass(context, DeliverComment.class);
                        i.putExtra("blogid", blogId);
                        i.putExtra("commentid", arrayList.get(position).getComment_id());
                        i.putExtra("commenterid", arrayList.get(position).getRecommentList().get(position1).getUser_id());
                        i.putExtra("nickname", arrayList.get(position).getRecommentList().get(position1).getNickname());
                        ((BaseActivity) context).startActivityForResult(i, position);
                    } else {
                        ToastUtils.shortToast("不能评论自己");
                    }
                } else {
                    ((BaseActivity) context).login(context);
                }
            }
        });

        return convertView;
    }


    //动态计算二级评论的高度
    private void setPullLvHeight(ListView pull, Adapter adapter, View footerView) {
        int totalHeight = 0;
        for (int i = 0, len = adapter.getCount(); i < len; i++) { //listAdapter.getCount()返回数据项的数目
            View listItem = adapter.getView(i, null, pull);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); //统计所有子项的总高度
        }
        ViewGroup.LayoutParams params = pull.getLayoutParams();
        params.height = totalHeight + (pull.getDividerHeight() * (pull.getCount() - 1));
        if (footerView != null) {
            footerView.measure(0, 0);
            params.height += footerView.getMeasuredHeight();
        }
        pull.setLayoutParams(params);
    }


    class MyHolder {
        RelativeLayout topic_detail_item_headLayout;
        TextView topic_detail_item_name;
        TextView topic_detail_item_time;
        TextView topic_detail_item_content;
        RoundedImageView topic_detail_item_head_ima;
        LinearLayout topic_detail_item_parent_layout;
        ListView topic_detail_item_second_comment_lv;
        LinearLayout topic_detail_recomment_list_footer;
        TextView topic_detail_recomment_list_footer_tv;
        TextView topic_detail_item_delete;
        MyAdapter myAdapter;
    }

    class MyAdapter extends BaseAdapter {

        private List<TopicDetailBean.CommentListBean.RecommentListBean> recommentListBeanList;

        public MyAdapter(List<TopicDetailBean.CommentListBean.RecommentListBean> recommentListBeanList) {
            this.recommentListBeanList = recommentListBeanList;
        }

        @Override
        public int getCount() {
            return recommentListBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyHolder myHolder;
            if (convertView == null) {
                myHolder = new MyHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.topic_detail_adapter_item, null);
                myHolder.topic_detail_adapter_item_tv = (MyVariousLineTextView) convertView.findViewById(R.id.topic_detail_adapter_item_tv);
                myHolder.flag = recommentListBeanList.get(position).getCommentedid();
                convertView.setTag(myHolder);
            } else {
                myHolder = (MyHolder) convertView.getTag();
            }
            if (TextUtils.isEmpty(myHolder.flag)) {
                //直接评论
                MyClickableSpan myClickableSpan = new MyClickableSpan(recommentListBeanList.get(position).getNickname(), context);
                SpannableString spannableString = new SpannableString(recommentListBeanList.get(position).getNickname() + ":" + recommentListBeanList.get(position).getContent());
                spannableString.setSpan(myClickableSpan, 0, recommentListBeanList.get(position).getNickname().length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                myHolder.topic_detail_adapter_item_tv.setText(spannableString);
            } else {
                //回复评论
                MyClickableSpan myClickableSpan = new MyClickableSpan(recommentListBeanList.get(position).getNickname(), context);
                MyClickableSpan myClickableSpan2 = new MyClickableSpan(recommentListBeanList.get(position).getCommentednickname(), context);
                SpannableString spannableString = new SpannableString(recommentListBeanList.get(position).getNickname() + " 回复@ " + recommentListBeanList.get(position).getCommentednickname() + " : " + recommentListBeanList.get(position).getContent());
                spannableString.setSpan(myClickableSpan, 0, recommentListBeanList.get(position).getNickname().length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(myClickableSpan2, recommentListBeanList.get(position).getNickname().length() + " 回复@ ".length(), recommentListBeanList.get(position).getCommentednickname().length() + recommentListBeanList.get(position).getNickname().length() + " 回复@ ".length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                myHolder.topic_detail_adapter_item_tv.setText(spannableString);
            }
            return convertView;
        }

        class MyHolder {
            MyVariousLineTextView topic_detail_adapter_item_tv;
            String flag;
        }
    }

    public interface DeleteCommentListener {
        void deleteCommentListener(int position);
    }

}
